package com.haiyou.dc.api.controller;

import com.haiyou.common.util.IdWorker;
import com.haiyou.common.util.R;
import com.haiyou.dc.api.constant.PayConstant;
import com.haiyou.dc.api.entity.Orders;
import com.haiyou.dc.api.service.OrdersService;
import com.haiyou.mini.programs.model.PayInfo;
import com.haiyou.mini.programs.service.WxMiniPayNotifyService;
import com.haiyou.mini.programs.service.WxMiniPayService;
import com.haiyou.mini.programs.strategy.ContextStrategy;
import com.haiyou.mini.programs.wxpay.WxPayXmlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author root
 * @date 2020-01-04 18:37:46
 */
@RestController
@RequestMapping("/pay")
@Slf4j
@Api(tags = "测试小程序支付")
@Validated
public class PayController {

    @Autowired
    private WxMiniPayService wxMiniPayService;
    @Autowired
    private WxMiniPayNotifyService wxMiniPayNotifyService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ContextStrategy contextStrategy;

    @ApiOperation(value = "小程序支付")
    @GetMapping("testPay")
    public R testPay() {
        PayInfo payInfo = new PayInfo();
        payInfo.setBody("小程序支付测试");
        payInfo.setOrderNo(IdWorker.getId());
        payInfo.setAttach("payOrder");
        payInfo.setTotalPrice(new BigDecimal(0.01));
        payInfo.setOpenid("ogukd0c9-H1R650arNWKfhuKDXzg");
        Map<String, String> map = wxMiniPayService.doUnifiedOrder(payInfo);
        return R.ok(map);
    }

    @ApiOperation(value = "支付回调", hidden = true)
    @RequestMapping(value = "/wxNotify")
    public void wxNotify(HttpServletResponse response) throws Exception {
        wxMiniPayNotifyService.wxNotify(notifyMap -> {

            //商户号
            String mch_id = notifyMap.get("mch_id");
            //用户标识
            String openid = notifyMap.get("openid");

            String total_fee = notifyMap.get("total_fee");
            //微信支付订单号
            String transaction_id = notifyMap.get("transaction_id");

            String attach = notifyMap.get("attach");

            //商户订单号
            String out_trade_no = notifyMap.get("out_trade_no");
            //查询订单 根据订单号查询订单
            Orders orders = ordersService.selectOne(new Orders().setOrderNo(out_trade_no));
            if (orders == null) {
                log.error("----->支付失败,错误信息：{}订单不存在", out_trade_no);
                WxPayXmlUtil.sendWxNoticeMessage("FAIL", "订单不存在", response);
                throw new RuntimeException("订单不存在");
            }

            //支付的状态判断
            if (PayConstant.PayStatusEnum.PAID.getType().equals(orders.getStatus())) {
                log.error("------>订单已处理");
                WxPayXmlUtil.sendWxNoticeMessage("SUCCESS", "OK", response);
                throw new RuntimeException("订单已处理");
            }

            contextStrategy.executeStrategy(attach);

        });
    }

    @ApiOperation(value = "小程序支付回调处理")
    @GetMapping("testOrderXx")
    public R testOrderXx() {
        contextStrategy.executeStrategy("order");
        return R.ok();
    }


}